<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * 用于缓存加载的工厂类
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
abstract class EGP_Cache
{

    /**
     * 实例化一个缓存适配器
     *
     * @param  string  $adapter  缓存适配器名称
     * @param  array   $config   缓存配置
     * @return EGP_Cache_Abstract
     * @throws EGP_Exception
     */
    public static function factory($adapter = 'file', $config = array())
    {
        if ($config instanceof EGP_ArrayObject)
            $config = $config->toArray();
        if (!is_array($config))
            throw new EGP_Exception('缓存配置必须是一个数组(Array)');
        $class = 'EGP_Cache_' . ucfirst($adapter);
        if (!EGP_Core::classExists($class))
            throw new EGP_Exception("不支持的缓存适配器: '$adapter'");
        $object = new $class($config);
        if (!$object instanceof EGP_Cache_Abstract)
            throw new EGP_Exception("缓存适配器 '$adapter' 必须从 'EGP_Cache_Abstract' 继承");
        return $object;
    }

}